package com.haoshuang.zhouzhoubang.utils

import android.content.Context
import com.blankj.utilcode.util.PermissionUtils
import com.haoshuang.zhouzhoubang.ui.dialog.PermissionTipDialog
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation

/**
 * 权限请求
 */
object PermissionHelper {

    fun requestPermission(
        content: Context?,
        permissionTitle: String,
        permissionContent: String,
        vararg permissions: String,
        callback: (isGranted: Boolean) -> Unit,
    ) {
        val isGranted = PermissionUtils.isGranted(*permissions)
        if (isGranted) {
            // 权限已被授权
            callback(true)
        } else {
            val popupView = XPopup.Builder(content)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .isDestroyOnDismiss(true)
                .popupAnimation(PopupAnimation.TranslateFromTop)
                .asCustom(PermissionTipDialog(content!!, permissionTitle, permissionContent))
                .show()

            PermissionUtils.permission(*permissions)
                .callback { isAllGranted, granted, deniedForever, denied ->
                    popupView.dismiss()
                    callback(isAllGranted)
                }
                .request()
        }
    }

}